package six.event.gua;

import cn.hutool.core.collection.CollUtil;
import six.event.trigrams.Trigrams;
import six.event.trigrams.TrigramsResultHandler;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author jiangjinjian
 * @date 2024/5/9 9:35
 * @description 卦象生成器
 */
public class GuaGenerator {

    private final Trigrams trigrams;

    private final TrigramsResultHandler handler;

    public GuaGenerator(Trigrams trigrams, TrigramsResultHandler handler) {
        this.trigrams = trigrams;
        this.handler = handler;
    }

    private List<String> getYaoList() {
        //获取六个爻
        List<String> reverseYaoList = Stream.iterate(0, t -> t + 1)
                //起卦
                .limit(6).map(i -> trigrams.trigrams())
                //获取爻
                .map(handler::handle).collect(Collectors.toList());
        //因为获取爻的过程是从下至上的，所以需要倒置，才是正确的顺序
        return CollUtil.reverse(reverseYaoList);
    }

    public void display() {
        getYaoList().forEach(System.out::println);
    }
}
